<?php
/**
 * @title 146. LRU 缓存
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class LRUCache {
    /**
     * @param Integer $capacity
     */
    function __construct($capacity) {
        /**
         * @since 2023-12-27 缓存容量
         */
        $this->maxLength = $capacity;

        /**
         * @since 2023-12-27 根据元素最近使用排序的key数组
         * @since 2023-12-27 哈希记录key对应的值和位置
         */
        $this->keyArray = $this->hashArray = [];

        /**
         * @since 2023-12-27 key数组对应指针
         */
        $this->keyPoint = 0;
    }

    /**
     * @param Integer $key
     * @return Integer
     */
    function get($key) {
        // echo "GET, key={$key}\n";
        // var_dump($this->keyArray);
        if(isset($this->hashArray[$key])){
            /**
             * @since 2023-12-27 获取对应值和在keyArray的位置
             */
            list($val, $position) = $this->hashArray[$key];
            // echo "val={$val}, position={$position}\n";

            /**
             * @since 2023-12-27 val最近使用，重新入keyArray
             */
            unset($this->keyArray[$position]);
            $this->keyArray[$this->keyPoint] = $key;

            /**
             * @since 2023-12-27 更新哈希数组key的position
             */
            $this->hashArray[$key][1] = $this->keyPoint;

            /**
             * @since 2023-12-27 移动指针
             */
            $this->keyPoint++;
            // echo "keys = ", implode(",", $this->keyArray), PHP_EOL;
        } else {
            $val = -1;
        }

        /**
         * @return Int
         */
        return $val;
    }

    /**
     * @param Integer $key
     * @param Integer $value
     * @return NULL
     */
    function put($key, $value) {
        // echo "PUT, key={$key}, value={$value}\n";
        // echo "keys = ", implode(",", $this->keyArray), PHP_EOL;

        /**
         * @since 2023-12-27 key存在则更新
         */
        if(isset($this->hashArray[$key])){
            $this->hashArray[$key][0] = $value;

            /**
             * @since 2023-12-27 存在更新，当相于get一次
             */
            $this->get($key);
        } else {
            /**
             * @since 2023-12-27 容量满了，需要移出1个元素
             */
            if(count($this->keyArray) == $this->maxLength){
                /**
                 * @since 2023-12-27 最少使用的key
                 */
                $keyKey = key($this->keyArray);
                $removeKey = $this->keyArray[$keyKey];
                unset($this->keyArray[$keyKey]);
                unset($this->hashArray[$removeKey]);
            } else {}

            /**
             * @since 2023-12-27 添加新的元素
             */
            $this->keyArray[$this->keyPoint] = $key;
            $this->hashArray[$key] = [$value, $this->keyPoint];
            $this->keyPoint++;
            // echo "keys = ", implode(",", $this->keyArray), PHP_EOL;
        }
    }
}

/**
 * Your LRUCache object will be instantiated and called as such:
 * $obj = LRUCache($capacity);
 * $ret_1 = $obj->get($key);
 * $obj->put($key, $value);
 */


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.27_6.php
 */
$datas = [

];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
